package cn.edu.scau.cmi.yangzhengkai.client.classicsDesignPattern;


import java.util.ArrayList;
import cn.edu.scau.cmi.yangzhengkai.adapter.Adaptee;
import cn.edu.scau.cmi.yangzhengkai.adapter.AdapterOfClass;
import cn.edu.scau.cmi.yangzhengkai.adapter.AdapterOfObject;
import cn.edu.scau.cmi.yangzhengkai.adapter.homework.SortClassAdapter;
import cn.edu.scau.cmi.yangzhengkai.adapter.homework.Student;
import cn.edu.scau.cmi.yangzhengkai.interProgramming.GraduateStudent;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class AdapterOfClassClient extends Application{

	ArrayList<Student> students = new ArrayList<Student>();
	private ListView<Text> lv = new ListView<>();
	@Override
	public void start(Stage primaryStage){
		FlowPane fp = new FlowPane();
		Button b1 = new Button("添加学生");
		b1.setPrefSize(200.0, 50.0);
		Button b2 = new Button("学生排序");
		b2.setPrefSize(200.0, 50.0);
		
		
		
		b1.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				addStudent();
				
			}
		});
		
		b2.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				sortStudent();
				
			}
		});
		VBox vb = new VBox();
		vb.getChildren().addAll(b1,b2);
		vb.setPadding(new Insets(100.0, 0.0, 0.0, 200.0));
		vb.setSpacing(50.0);
		fp.getChildren().addAll(vb);
		Scene scene = new Scene(fp);
		primaryStage.setScene(scene);
		primaryStage.setHeight(400);
		primaryStage.setWidth(600);
		primaryStage.show();
		
	}
	
	public boolean isExisted(int number) {
		boolean flag = false;
		for(int i = 0;i < students.size(); i++) {
			if(number == students.get(i).getNumber()) {
				flag = true;
				break;
			}
			
		}
		return flag;
	}
	
	public void addStudent(){
		Pane pane = new Pane();
		TextField tf1 = new TextField();
		tf1.setPromptText("请输入学生学号:");
		
		TextField tf2 = new TextField();
		tf2.setPromptText("请输入学生姓名:");
		
		Button bu = new Button("确定");
		bu.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				if(tf1.getText().equals("")||tf2.getText().equals("")) {
					showMessage("请输入完整信息!");
				}
				else if(isExisted(Integer.valueOf(tf1.getText()))) {
					showMessage("该学生学号已存在!");
				}
				else {
				Student student = new Student(Integer.valueOf(tf1.getText()), tf2.getText());
				students.add(student);
				showMessage("添加成功!");
				}
			}
		});
		VBox vb = new VBox();
		vb.getChildren().addAll(tf1,tf2,bu);
		vb.setPadding(new Insets(100.0, 0.0, 0.0, 200.0));
		vb.setSpacing(50.0);
		pane.getChildren().add(vb);
		Scene scene = new Scene(pane);
		Stage stage = new Stage();
		stage.setScene(scene);
		stage.setWidth(600);
		stage.setHeight(600);		
		stage.show();
		
	}
	
	public void sortStudent(){
		SortClassAdapter sca = new SortClassAdapter();
		sca.sortStudents(students);
		lv.getItems().add(new Text("学号" + "\t\t\t" + "名字"));
		lv.setPrefSize(430, 280);
		lv.setFixedCellSize(25);
		lv.setLayoutX(60);
		lv.setLayoutY(150);
		lv.getItems().clear();
		lv.getItems().add(new Text("学号" + "\t\t\t" + "名字"));
		for(int i=0; i<students.size(); i++) {
			Student temp = students.get(i);
			Text text = new Text(temp.getNumber() + "\t\t\t" + temp.getName());
			text.setFill(Color.DODGERBLUE);
			lv.getItems().add(text);
		}
		Pane pane = new Pane();
		pane.getChildren().add(lv);
		Scene scene = new Scene(pane);
		Stage stage = new Stage();
		stage.setScene(scene);
		stage.setWidth(600);
		stage.setHeight(600);		
		stage.show();
		
	}
	
	public void showMessage(String message) {
		
		VBox vBox = new VBox(60);
		vBox.setAlignment(Pos.CENTER);
		vBox.setPrefSize(300, 250);
		
		Text text = new Text(message);
		text.setStyle("-fx-font-size: 25"); 
			
		Button button = new Button("确定");
		button.setPrefSize(60, 30);
		vBox.getChildren().addAll(text, button);
			
		Stage stage = new Stage();
		Scene scene = new Scene(vBox);
		stage.setScene(scene);
		stage.show();
			
		
		button.setOnMouseClicked(e -> stage.close() ); 		
	}
	
	public static void main(String[] args) {
		launch(args);
	}

}
